package com.sgcc.operate.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.sgcc.common.core.domain.R;
import com.sgcc.common.security.service.TokenService;
import com.sgcc.operate.domain.*;
import com.sgcc.operate.mapper.*;
import com.sgcc.system.api.RemoteDictDataService;
import com.sgcc.system.api.model.LoginUser;
import org.apache.commons.lang3.StringUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sgcc.operate.service.IOActiInfoService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 活动信息Service业务层处理
 *
 * @author sgcc
 * @date 2021-03-23
 */
@Service
public class OActiInfoServiceImpl implements IOActiInfoService {
    @Autowired
    private OActiInfoMapper oActiInfoMapper;
    @Autowired
    private RemoteDictDataService remoteDictDataService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private OIndiContentMapper oIndiContentMapper;
    @Autowired
    private ORuleAttrMapper oRuleAttrMapper;
    @Autowired
    private OPrizeCostMapper oPrizeCostMapper;
    @Autowired
    private OActiRuleMapper oActiRuleMapper;
    @Autowired
   private OActiPrizeMapper oActiPrizeMapper;
    @Autowired
    private OPutChannelMapper oPutChannelMapper;

    /**
     * 查询活动信息
     *
     * @param id 活动信息ID
     * @return 活动信息
     */
    @Override
    public OActiInfo selectOActiInfoById(String id) {
        OActiInfo oActiInfo = oActiInfoMapper.selectOActiInfoById(id);
        if(oActiInfo==null){
            return null;
        }
        StringBuilder sb = new StringBuilder();
//        for (OPutChannel channel : putChannelMapper.queryListByActi(oActiInfo.getId())) {
//            sb.append(channel.getPutChannel()).append(",");
//        }
        OPrizeCost oPrizeCost = new OPrizeCost();
        oPrizeCost.setActiid(oActiInfo.getId());
        oActiInfo.setChannelsStr(sb.toString());
        oActiInfo.setOprizeCostList(oPrizeCostMapper.selectOPrizeCostList(oPrizeCost));
        OActiRule oActiRule = new OActiRule();
        oActiRule.setActiid(oActiInfo.getId());
        OActiRule byActiId = oActiRuleMapper.getByActiId(oActiInfo.getId());
        if(byActiId!=null){
            OPutChannel oPutChannel = new OPutChannel();
            oPutChannel.setActiId(id);
            List<OPutChannel> oPutChannels = oPutChannelMapper.selectOPutChannelList(oPutChannel);
//            byActiId.setChannels(oPutChannels);
            StringBuilder sb1 = new StringBuilder();
            for (OPutChannel channel : oPutChannels) {
                sb1.append(channel.getPutChannel()).append(",");
            }
            byActiId.setChannelsStr(sb1.toString());
            byActiId.setOprizeCostList(oPrizeCostMapper.selectOPrizeCostList(oPrizeCost));
        }
        oActiInfo.setoActiRule(byActiId);
        return oActiInfo;
    }

    /**
     * 查询活动信息列表
     *
     * @param oActiInfo 活动信息
     * @return 活动信息
     */
    @Override
    public List<OActiInfo> selectOActiInfoList(OActiInfo oActiInfo) {
        return oActiInfoMapper.selectOActiInfoList(oActiInfo);
    }

    /**
     * 新增活动信息
     *
     * @param oActiInfo 活动信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertOActiInfo(OActiInfo oActiInfo) {
        LoginUser loginUser = tokenService.getLoginUser();
        oActiInfo.setCreateDate(new Date());
        oActiInfo.setUpdateDate(new Date());
        oActiInfo.setCreateBy(loginUser.getUserid());
        oActiInfo.setUpdateBy(loginUser.getUserid());
        oActiInfo.setDelFlag("0");
        oActiInfo.setActistatus("01");
        //通过运营指标制定id ，添加中间表,获取指标内容的id
        if (oActiInfo.getIndicontentid() == null) { //指标内容表为空
            OIndiContent oIndiContent = new OIndiContent();
            oIndiContent.setIndiid(oActiInfo.getOindiformid());
            OSchemeForm oSchemeForm = new OSchemeForm();
            oSchemeForm.setId(oActiInfo.getSchemeid());
            oIndiContent.setSchemeid(oSchemeForm.getId());
            String id = UUID.randomUUID().toString().replaceAll("-", "");
            oIndiContent.setId(id);
            oIndiContent.setCreateDate(new Date());
            oIndiContent.setUpdateDate(new Date());
            oIndiContent.setCreateBy(loginUser.getUserid());
            oIndiContent.setUpdateBy(loginUser.getUserid());
            oIndiContent.setDelFlag("0");
            oIndiContentMapper.insertOIndiContent(oIndiContent);

            //活动表关联指标内容id
            oActiInfo.setIndicontentid(oIndiContent.getId());
        }
        try {
                R idstr = remoteDictDataService.getIdStr("same_day_id_create", "acti_info_id");
                if (idstr.getCode() == 200) {
                    oActiInfo.setId("FAHD" + idstr.getData() + "");
                } else {
                    return 0;
                }
                oActiInfo.setUpdateBy(loginUser.getUserid());
                oActiInfo.setCreateBy(loginUser.getUserid());
                oActiInfo.setActistatus("01");
                oActiInfo.setUpdateDate(new Date());
                oActiInfo.setCreateDate(oActiInfo.getUpdateDate());
                oActiInfoMapper.insertOActiInfo(oActiInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (oActiInfo.getOprizeCostList() != null) {
            //获取奖品列表，并删除
            OPrizeCost orizeCost = new OPrizeCost();
            orizeCost.setActiid(oActiInfo.getId());
            List<OPrizeCost> list = oPrizeCostMapper.selectOPrizeCostList(orizeCost);
            for (OPrizeCost oPrizeCost : list) {
                oPrizeCostMapper.deleteOPrizeCostById(oPrizeCost.getId());
            }
            double prizecost = 0;
            double othercost = 0;
            for (OPrizeCost oPrizeCost : oActiInfo.getOprizeCostList()) {
                if (StringUtils.isBlank(oPrizeCost.getPrizetype())) {
                    continue;
                }
//                if ("99".equals(oPrizeCost.getPrizetype()) && (StringUtils.isBlank(oPrizeCost.getPrizename()) || StringUtils.isBlank(oPrizeCost.getPrizeamount()))) {
//                    continue;
//                }
//				else if ("99".equals(oPrizeCost.getPrizetype())&&(StringUtils.isNotBlank(oPrizeCost.getPrizename())&&StringUtils.isNotBlank(oPrizeCost.getPrizeamount()))) {
//					oPrizeCost.setPrizeamount(oPrizeCost.getPrizeamount());
//				}else if ("01".equals(oPrizeCost.getPrizetype())&&StringUtils.isNotBlank(oPrizeCost.getPrizeprice())) {
//					double amount = Double.valueOf(oPrizeCost.getPrizeprice())*Double.valueOf(oPrizeCost.getPrizequant());
//					oPrizeCost.setPrizeamount(String.valueOf(amount));
//				}
                oPrizeCost.setActiid(oActiInfo.getId());
                oPrizeCost.setCreateDate(new Date());
                oPrizeCost.setUpdateDate(new Date());
                oPrizeCost.setUpdateBy(loginUser.getUserid());
                oPrizeCost.setCreateBy(loginUser.getUserid());
                String id = UUID.randomUUID().toString().replaceAll("-", "");
                oPrizeCost.setId(id);
                oPrizeCost.setDelFlag("0");
                oPrizeCostMapper.insertOPrizeCost(oPrizeCost);
            }

            //更新活动表中  奖品费用信息
//			oActiInfo.setPrizecost(String.valueOf(prizecost));
//			oActiInfo.setOthercost(String.valueOf(othercost));
//			oActiInfo.setActibudget(String.valueOf(prizecost+othercost));
//			oActiInfoMapper.update(oActiInfo);
        }
        return 1;


    }

    /**
     * 修改活动信息
     *
     * @param oActiInfo 活动信息
     * @return 结果
     */
    @Override
    public int updateOActiInfo(OActiInfo oActiInfo) {
        LoginUser loginUser = tokenService.getLoginUser();
        oActiInfo.setUpdateDate(new Date());
        oActiInfo.setUpdateBy(loginUser.getUserid());
        oActiInfo.setDelFlag("0");
        oActiInfo.setActistatus("01");
        //通过运营指标制定id ，添加中间表,获取指标内容的id
        if (oActiInfo.getIndicontentid() == null) { //指标内容表为空
            OIndiContent oIndiContent = new OIndiContent();
            oIndiContent.setIndiid(oActiInfo.getId());
            OSchemeForm oSchemeForm = new OSchemeForm();
            oSchemeForm.setId(oActiInfo.getSchemeid());
            oIndiContent.setSchemeid(oSchemeForm.getId());
            String id = UUID.randomUUID().toString().replaceAll("-", "");
            oIndiContent.setId(id);
            oIndiContentMapper.insertOIndiContent(oIndiContent);

            //活动表关联指标内容id
            oActiInfo.setIndicontentid(oIndiContent.getId());
        }
        try {

            oActiInfoMapper.updateOActiInfo(oActiInfo);

        } catch (Exception e) {
            e.printStackTrace();
        }
        if (oActiInfo.getOprizeCostList() != null) {
            //获取奖品列表，并删除
            OPrizeCost orizeCost = new OPrizeCost();
            orizeCost.setActiid(oActiInfo.getId());
            List<OPrizeCost> list = oPrizeCostMapper.selectOPrizeCostList(orizeCost);
            for (OPrizeCost oPrizeCost : list) {
                oPrizeCostMapper.deleteOPrizeCostById(oPrizeCost.getId());
            }
            double prizecost = 0;
            double othercost = 0;
            for (OPrizeCost oPrizeCost : oActiInfo.getOprizeCostList()) {
                if (StringUtils.isBlank(oPrizeCost.getPrizetype())) {
                    continue;
                }
                if ("99".equals(oPrizeCost.getPrizetype()) && (StringUtils.isBlank(oPrizeCost.getPrizename()) || StringUtils.isBlank(oPrizeCost.getPrizeamount()))) {
                    continue;
                }
//				else if ("99".equals(oPrizeCost.getPrizetype())&&(StringUtils.isNotBlank(oPrizeCost.getPrizename())&&StringUtils.isNotBlank(oPrizeCost.getPrizeamount()))) {
//					oPrizeCost.setPrizeamount(oPrizeCost.getPrizeamount());
//				}else if ("01".equals(oPrizeCost.getPrizetype())&&StringUtils.isNotBlank(oPrizeCost.getPrizeprice())) {
//					double amount = Double.valueOf(oPrizeCost.getPrizeprice())*Double.valueOf(oPrizeCost.getPrizequant());
//					oPrizeCost.setPrizeamount(String.valueOf(amount));
//				}
                oPrizeCost.setActiid(oActiInfo.getId());
                oPrizeCost.setCreateDate(new Date());
                oPrizeCost.setUpdateDate(new Date());
                oPrizeCost.setUpdateBy(loginUser.getUserid());
                oPrizeCost.setCreateBy(loginUser.getUserid());
                oPrizeCost.setDelFlag("0");
                String id = UUID.randomUUID().toString().replaceAll("-", "");
                oPrizeCost.setId(id);
                oPrizeCostMapper.insertOPrizeCost(oPrizeCost);
            }

            //更新活动表中  奖品费用信息
//			oActiInfo.setPrizecost(String.valueOf(prizecost));
//			oActiInfo.setOthercost(String.valueOf(othercost));
//			oActiInfo.setActibudget(String.valueOf(prizecost+othercost));
//			oActiInfoMapper.update(oActiInfo);
        }

        return 1;
    }

    /**
     * 批量删除活动信息
     *
     * @param ids 需要删除的活动信息ID
     * @return 结果
     */
    @Override
    public int deleteOActiInfoByIds(String[] ids) {
        return oActiInfoMapper.deleteOActiInfoByIds(ids);
    }

    /**
     * 删除活动信息信息
     *
     * @param id 活动信息ID
     * @return 结果
     */
    @Override
    public int deleteOActiInfoById(String id) {
        return oActiInfoMapper.deleteOActiInfoById(id);
    }

    @Override
    public List<OActiInfo> actiManageList(OActiInfo oActiInfo) {
        return oActiInfoMapper.actiManageList(oActiInfo);
    }

    @Override
    public List<OActiInfo> oActiRuleList(OActiInfo oActiInfo) {
        return oActiInfoMapper.getActiRuleList(oActiInfo);
    }

    @Override
    public List<Map<String, Integer>> prizeDetails(String id) {
        return oActiPrizeMapper.prizeDetails(id);
    }

    @Override
    public void update(OActiInfo update) {
        oActiInfoMapper.updateOActiInfo(update);
    }
}
